package cn.doctor.assistant.pad.ui.ziliao;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.doctor.assistant.pad.R;
import cn.doctor.assistant.pad.api.ziliao.InformatApi;
import cn.doctor.assistant.pad.api.ziliao.RecordResponse;
import cn.doctor.assistant.pad.base.BaseNetActivity;
import cn.doctor.assistant.pad.base.ResultCallback;
import cn.doctor.assistant.pad.entity.Patient;
import cn.doctor.assistant.pad.entity.Record;
import cn.doctor.assistant.pad.ui.UI;

/**
 * 患者资料详情页
 */
public class PatientDetailActivity extends BaseNetActivity {

    @InjectView(R.id.titlebar_left_title)
    TextView title;
    @InjectView(R.id.titlebar_right_text)
    TextView rightText;
    @InjectView(R.id.icon)
    ImageView icon;
    @InjectView(R.id.text_name)
    TextView textName;
    @InjectView(R.id.text_sex)
    TextView textSex;
    @InjectView(R.id.text_marriage)
    TextView textMarriage;
    @InjectView(R.id.text_brithday)
    TextView textBrithday;
    @InjectView(R.id.text_phone)
    TextView textPhone;
    @InjectView(R.id.text_address)
    TextView textAddress;
    @InjectView(R.id.text_note)
    TextView textNote;
    @InjectView(R.id.listview)
    ListView listview;

    RecordAdapter mAdapter;
    Patient patient;
    List<Record> records = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_patient_detail);
        ButterKnife.inject(this);
        setTitle("病人资料详情");
        setRightTitleBarText("新增住院记录");

        patient = (Patient) getIntent().getExtras().getSerializable("patient");
        initialize();
    }

    void initialize() {
        textName.setText(patient.getName());
        textSex.setText("性别：" + patient.getSexString());
        textMarriage.setText("婚姻状况：" + patient.getMarriageString());
        textBrithday.setText("出生日期：" + getStr(patient.getBirthday()));
        textPhone.setText("手机号：" + getStr(patient.getPhone()));
        textAddress.setText("联系地址：" + getStr(patient.getAddress()));
        textNote.setText("备注信息：" + (TextUtils.isEmpty(patient.getNote()) ? "暂无" : patient.getNote()));

        getRecord();

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                UI.startActivity(ZhuYuanDetailActivity.class, "record", records.get(position));
            }
        });
    }

    void getRecord() {
        InformatApi.getRecordWithUid(patient.getId() + "", 1, 20, new ResultCallback<RecordResponse>() {
            @Override
            public void onSuccess(RecordResponse result) {
                if (result.getRecords().size() > 0) {
                    records.addAll(result.getRecords());
                    if (mAdapter == null) {
                        mAdapter = new RecordAdapter();
                        listview.setAdapter(mAdapter);
                    } else {
                        mAdapter.notifyDataSetChanged();
                    }
                } else {
                    showToast("无病案信息");
                }
            }
        });
    }

    String getStr(String text) {
        if (TextUtils.isEmpty(text)) {
            return "";
        } else {
            return text;
        }
    }

    @OnClick({R.id.titlebar_left_title, R.id.titlebar_right_text})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.titlebar_left_title:
                back();
                break;
            case R.id.titlebar_right_text:
                //UI.startActivity(AddZhuYuanActivity.class, "patient", patient);
                Intent intent = new Intent(this, AddZhuYuanActivity.class);
                intent.putExtra("patient", patient);
                startActivityForResult(intent, 1);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == 1) {
            Record record = (Record) data.getExtras().getSerializable("record");
            records.add(record);
            mAdapter.notifyDataSetChanged();
        }
    }

    class RecordAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return records.size();
        }

        @Override
        public Object getItem(int position) {
            return records.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            Record record = records.get(position);

            if (convertView == null) {
                convertView = LayoutInflater.from(PatientDetailActivity.this)
                        .inflate(R.layout.list_item_patient_detail_record, parent, false);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.listItemId.setText(String.valueOf(position + 1));
            holder.listItemKeshi.setText(record.getDept());
            holder.listItemDiagnose.setText(record.getNote());
            String startDate = record.getAdmissionDate();
            holder.listItemStartDate.setText(startDate.substring(0, 10));
            String endDate = record.getDischargeDate();
            if (!TextUtils.isEmpty(endDate)) {
                holder.listItemEndDate.setText(endDate.substring(0, 10));
            }

            return convertView;
        }


    }

    class ViewHolder {
        @InjectView(R.id.list_item_id)
        TextView listItemId;
        @InjectView(R.id.list_item_keshi)
        TextView listItemKeshi;
        @InjectView(R.id.list_item_diagnose)
        TextView listItemDiagnose;
        @InjectView(R.id.list_item_start_date)
        TextView listItemStartDate;
        @InjectView(R.id.list_item_end_date)
        TextView listItemEndDate;

        ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }
}
